﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;

namespace SmartEP.Model
{
    public class XMLModel
    {
        protected ArrayList ReadXML(string FileName)
        {
            String path = "XML\\" + FileName.ToString();
            if (!File.Exists(path)) return null;
            int count = 0;
            ArrayList Arr = new ArrayList();
            XmlTextReader xml = new XmlTextReader(path);
            while (xml.Read())
            {
                switch (xml.NodeType)
                {
                    case XmlNodeType.Text:
                        path = Convert.ToString(xml.Value);
                        if (path != "")
                        {
                            Arr.Insert(count, path);
                            count++;
                        }
                            break;
                }
            }
            xml.Close();
            return Arr;
        }

        protected void WriteDatabaseXML(string FileName, string ServerName, string DatabaseName, string Username, string Password)
        {
            try
            {
                string path ="XML\\" + FileName.ToString();
                if (!File.Exists(path)) return;
                XmlTextWriter xml = new XmlTextWriter(path, Encoding.UTF8);
                xml.Formatting = Formatting.Indented;
                xml.WriteStartDocument();
                xml.WriteStartElement("SmartEyePix");
                FileName = FileName.Replace(".xml", "").ToUpper();
                xml.WriteStartElement("ConnectDB");
                xml.WriteElementString("ServerName",ServerName);
                xml.WriteElementString("DatabaseName",DatabaseName);
                xml.WriteElementString("UserName", Username);
                xml.WriteElementString("Password", Password);
                xml.WriteEndElement();//ConnectDB

                xml.WriteEndElement();//SmartEyePix
                xml.WriteEndDocument();
                xml.Close();
            }
            catch { }
        }

        protected void WriteLoginXML(string FileName, string Username, string Password)
        {
            try
            {
                string path = "XML\\" + FileName.ToString();
                if (!File.Exists(path)) return;
                XmlTextWriter xml = new XmlTextWriter(path, Encoding.UTF8);
                xml.Formatting = Formatting.Indented;
                xml.WriteStartDocument();
                xml.WriteStartElement("SmartEyePix");
                FileName = FileName.Replace(".xml", "").ToUpper();
                xml.WriteStartElement("Login");
                xml.WriteElementString("UserName", Username);
                xml.WriteElementString("Password", "");
                xml.WriteEndElement();//Login

                xml.WriteEndElement();//SmartEyePix
                xml.WriteEndDocument();
                xml.Close();
            }
            catch { }
        }
    }
}
